= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 
 
 A u t h o r :   Y o u n g w o o k   K i m   ( K o r e a n ) 
 
 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 
 
 
 
 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 
 
 C o n t a c t :   r u m i a 0 6 0 1 @ g m a i l . c o m 
 
 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 
 
 
 
 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 
 
 w i t h   A d v a n c e d   O U T P U T      B u t t o n s 
 
 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 
 
 
 
 E v e n   i f   K E Y D O W N   e v e n t   i s   u s e d ,   i t   s e e m s   l i k e   t h i s   i s   n o t   e n t i r e l y   G U I   g a m e   b e c a u s e   G U I   o f   t h i s   g a m e   i s   o n l y   u s e d   f o r   o u t p u t ( = p r i n t ) ,   n o t   i n p u t .   I n p u t   f o r   G U I   m e a n s   c a r i n g   m o u s e   e v e n t   f o r   s p e c i f i c   l o c a t i o n .   H o w   a b o u t   m a k i n g   t w o   b u t t o n s   t o   i n c r e a s e   o r   d e c r e a s e   H P ? 
 
 
 
 . .   i m a g e : :   A d v a n c e d I n p u t O u t p u t 1 . g i f 
 
       : c l a s s :   i n l i n e d - r i g h t 
 
 
 
 . .   c o d e - b l o c k : :   p y t h o n 
 
       : l i n e n o s : 
 
 
 
       i m p o r t   s y s ,   p y g a m e 
 
       p y g a m e . i n i t ( ) 
 
 
 
       s i z e   =   w i d t h ,   h e i g h t   =   3 2 0 ,   2 4 0 
 
       s p e e d   =   [ 2 ,   2 ] 
 
       b l a c k   =   0 ,   0 ,   0 
 
 
 
       s c r e e n   =   p y g a m e . d i s p l a y . s e t _ m o d e ( s i z e ) 
 
 
 
       b a l l   =   p y g a m e . i m a g e . l o a d ( " A d v a n c e d I n p u t O u t p u t 1 . g i f " ) 
 
       b a l l r e c t   =   b a l l . g e t _ r e c t ( ) 
 
 
 
       w h i l e   1 : 
 
               f o r   e v e n t   i n   p y g a m e . e v e n t . g e t ( ) : 
 
                       i f   e v e n t . t y p e   = =   p y g a m e . Q U I T :   s y s . e x i t ( ) 
 
 
 
               b a l l r e c t   =   b a l l r e c t . m o v e ( s p e e d ) 
 
               i f   b a l l r e c t . l e f t   <   0   o r   b a l l r e c t . r i g h t   >   w i d t h : 
 
                       s p e e d [ 0 ]   =   - s p e e d [ 0 ] 
 
               i f   b a l l r e c t . t o p   <   0   o r   b a l l r e c t . b o t t o m   >   h e i g h t : 
 
                       s p e e d [ 1 ]   =   - s p e e d [ 1 ] 
 
 
 
               s c r e e n . f i l l ( b l a c k ) 
 
               s c r e e n . b l i t ( b a l l ,   b a l l r e c t ) 
 
               p y g a m e . d i s p l a y . f l i p ( ) 
 
 
 
 . .   i m a g e : :   A d v a n c e d I n p u t O u t p u t 2 . g i f 
 
       : c l a s s :   i n l i n e d - r i g h t 
 
 
 
 . .   c o d e - b l o c k : :   p y t h o n 
 
       : l i n e n o s : 
 
 
 
       i m p o r t   s y s ,   p y g a m e 
 
       p y g a m e . i n i t ( ) 
 
 
 
       s i z e   =   w i d t h ,   h e i g h t   =   3 2 0 ,   2 4 0 
 
       s p e e d   =   [ 2 ,   2 ] 
 
       b l a c k   =   0 ,   0 ,   0 
 
 
 
       s c r e e n   =   p y g a m e . d i s p l a y . s e t _ m o d e ( s i z e ) 
 
 
 
       b a l l   =   p y g a m e . i m a g e . l o a d ( " A d v a n c e d I n p u t O u t p u t 2 . g i f " ) 
 
       b a l l r e c t   =   b a l l . g e t _ r e c t ( ) 
 
 
 
       w h i l e   1 : 
 
               f o r   e v e n t   i n   p y g a m e . e v e n t . g e t ( ) : 
 
                       i f   e v e n t . t y p e   = =   p y g a m e . Q U I T :   s y s . e x i t ( ) 
 
 
 
               b a l l r e c t   =   b a l l r e c t . m o v e ( s p e e d ) 
 
               i f   b a l l r e c t . l e f t   <   0   o r   b a l l r e c t . r i g h t   >   w i d t h : 
 
                       s p e e d [ 0 ]   =   - s p e e d [ 0 ] 
 
               i f   b a l l r e c t . t o p   <   0   o r   b a l l r e c t . b o t t o m   >   h e i g h t : 
 
                       s p e e d [ 1 ]   =   - s p e e d [ 1 ] 
 
 
 
               s c r e e n . f i l l ( b l a c k ) 
 
               s c r e e n . b l i t ( b a l l ,   b a l l r e c t ) 
 
               p y g a m e . d i s p l a y . f l i p ( ) 
 
 
 
 O k a y ,   m a k i n g   t w o   b u t t o n s   i s   s i m p l e .   L o o k   a t   t h e   b u t t o n ,   t h e y   h a v e   u n i q u e   v i s u a l   s h a p e .   H o w   c a n   i t   b e ?   S i m p l e   a s   p r e v i o u s   i d e a :   F i r s t ,   d r a w   * * b i g   s q u a r e * * .   S e c o n d ,   d r a w   * * s m a l l e r   s q u a r e * *   w h i c h   h a s   s m a l l   w i d t h   ( t h i s   s q u a r e   d o e s n  t   h a v e   i n n e r   c o l o r   s o   c o l o r   o f   b i g   s q u a r e   c a n   b e   d i s p l a y e d )   s o   i n n e r   s q u a r e   a n d   o u t e r   s q u a r e   s e e m s   l i k e   s e p a r a t e d .   B u t   t h e s e   b u t t o n s   a r e   s t i l l   f o r   o u t p u t   o n l y .   W e   n e e d   t o   m a k e   c l i c k   a r e a   f o r   t h i s . 
 
 
 
 . .   i m a g e : :   A d v a n c e d I n p u t O u t p u t 3 . g i f 
 
       : c l a s s :   i n l i n e d - r i g h t 
 
 
 
 . .   c o d e - b l o c k : :   p y t h o n 
 
       : l i n e n o s : 
 
 
 
       i m p o r t   s y s ,   p y g a m e 
 
       p y g a m e . i n i t ( ) 
 
 
 
       s i z e   =   w i d t h ,   h e i g h t   =   3 2 0 ,   2 4 0 
 
       s p e e d   =   [ 2 ,   2 ] 
 
       b l a c k   =   0 ,   0 ,   0 
 
 
 
       s c r e e n   =   p y g a m e . d i s p l a y . s e t _ m o d e ( s i z e ) 
 
 
 
       b a l l   =   p y g a m e . i m a g e . l o a d ( " A d v a n c e d I n p u t O u t p u t 3 . g i f " ) 
 
       b a l l r e c t   =   b a l l . g e t _ r e c t ( ) 
 
 
 
       w h i l e   1 : 
 
               f o r   e v e n t   i n   p y g a m e . e v e n t . g e t ( ) : 
 
                       i f   e v e n t . t y p e   = =   p y g a m e . Q U I T :   s y s . e x i t ( ) 
 
 
 
               b a l l r e c t   =   b a l l r e c t . m o v e ( s p e e d ) 
 
               i f   b a l l r e c t . l e f t   <   0   o r   b a l l r e c t . r i g h t   >   w i d t h : 
 
                       s p e e d [ 0 ]   =   - s p e e d [ 0 ] 
 
               i f   b a l l r e c t . t o p   <   0   o r   b a l l r e c t . b o t t o m   >   h e i g h t : 
 
                       s p e e d [ 1 ]   =   - s p e e d [ 1 ] 
 
 
 
               s c r e e n . f i l l ( b l a c k ) 
 
               s c r e e n . b l i t ( b a l l ,   b a l l r e c t ) 
 
               p y g a m e . d i s p l a y . f l i p ( ) 
 
 
 
 . .   i m a g e : :   A d v a n c e d I n p u t O u t p u t 4 . g i f 
 
       : c l a s s :   i n l i n e d - r i g h t 
 
 
 
 . .   c o d e - b l o c k : :   p y t h o n 
 
       : l i n e n o s : 
 
 
 
       i m p o r t   s y s ,   p y g a m e 
 
       p y g a m e . i n i t ( ) 
 
 
 
       s i z e   =   w i d t h ,   h e i g h t   =   3 2 0 ,   2 4 0 
 
       s p e e d   =   [ 2 ,   2 ] 
 
       b l a c k   =   0 ,   0 ,   0 
 
 
 
       s c r e e n   =   p y g a m e . d i s p l a y . s e t _ m o d e ( s i z e ) 
 
 
 
       b a l l   =   p y g a m e . i m a g e . l o a d ( " A d v a n c e d I n p u t O u t p u t 4 . g i f " ) 
 
       b a l l r e c t   =   b a l l . g e t _ r e c t ( ) 
 
 
 
       w h i l e   1 : 
 
               f o r   e v e n t   i n   p y g a m e . e v e n t . g e t ( ) : 
 
                       i f   e v e n t . t y p e   = =   p y g a m e . Q U I T :   s y s . e x i t ( ) 
 
 
 
               b a l l r e c t   =   b a l l r e c t . m o v e ( s p e e d ) 
 
               i f   b a l l r e c t . l e f t   <   0   o r   b a l l r e c t . r i g h t   >   w i d t h : 
 
                       s p e e d [ 0 ]   =   - s p e e d [ 0 ] 
 
               i f   b a l l r e c t . t o p   <   0   o r   b a l l r e c t . b o t t o m   >   h e i g h t : 
 
                       s p e e d [ 1 ]   =   - s p e e d [ 1 ] 
 
 
 
               s c r e e n . f i l l ( b l a c k ) 
 
               s c r e e n . b l i t ( b a l l ,   b a l l r e c t ) 
 
               p y g a m e . d i s p l a y . f l i p ( ) 
 
 
 
 . .   i m a g e : :   A d v a n c e d I n p u t O u t p u t 5 . g i f 
 
       : c l a s s :   i n l i n e d - r i g h t 
 
 
 
 . .   c o d e - b l o c k : :   p y t h o n 
 
       : l i n e n o s : 
 
 
 
       i m p o r t   s y s ,   p y g a m e 
 
       p y g a m e . i n i t ( ) 
 
 
 
       s i z e   =   w i d t h ,   h e i g h t   =   3 2 0 ,   2 4 0 
 
       s p e e d   =   [ 2 ,   2 ] 
 
       b l a c k   =   0 ,   0 ,   0 
 
 
 
       s c r e e n   =   p y g a m e . d i s p l a y . s e t _ m o d e ( s i z e ) 
 
 
 
       b a l l   =   p y g a m e . i m a g e . l o a d ( " A d v a n c e d I n p u t O u t p u t 5 . g i f " ) 
 
       b a l l r e c t   =   b a l l . g e t _ r e c t ( ) 
 
 
 
       w h i l e   1 : 
 
               f o r   e v e n t   i n   p y g a m e . e v e n t . g e t ( ) : 
 
                       i f   e v e n t . t y p e   = =   p y g a m e . Q U I T :   s y s . e x i t ( ) 
 
 
 
               b a l l r e c t   =   b a l l r e c t . m o v e ( s p e e d ) 
 
               i f   b a l l r e c t . l e f t   <   0   o r   b a l l r e c t . r i g h t   >   w i d t h : 
 
                       s p e e d [ 0 ]   =   - s p e e d [ 0 ] 
 
               i f   b a l l r e c t . t o p   <   0   o r   b a l l r e c t . b o t t o m   >   h e i g h t : 
 
                       s p e e d [ 1 ]   =   - s p e e d [ 1 ] 
 
 
 
               s c r e e n . f i l l ( b l a c k ) 
 
               s c r e e n . b l i t ( b a l l ,   b a l l r e c t ) 
 
               p y g a m e . d i s p l a y . f l i p ( ) 
 
 
 
 N o w   n e w   e v e n t   ` ` M O U S E B U T T O N U P ` `   i s   a d d e d   a t   E v e n t   s t a t e m e n t .   N o t i c e   t h a t   u p   o f   m o u s e   i s   s a m e   a s   d o w n   o f   k e y .   I f   M O U S E B U T T O N U P   i s   a c t i v a t e d ,   ` ` e v e n t . p o s ` `   w i l l   b e   r e c o r d e d   a s   x   a n d   y .   W h i c h   m e a n s ,   c l i c k e d   p o i n t .   S o ,   d e t e r m i n i n g   c l i c k e d   p o i n t   i s   w h e t h e r   i n s i d e   o f   c e r t a i n   R e c t   a r e a   o r   n o t   i s   n e e d e d   b y   c h e c k i n g   ` ` c o l l i d e p o i n t ` ` .   I f   i n s i d e ,   i t   m e a n s    * * u s e r   c l i c k e d   s o m e   p o i n t   w h i c h   i s   p a r t   o f   c e r t a i n   a r e a * *    t h e n ,   a d e q u a t e   p r o c e s s   ( u p d a t i n g   v a l u e )   i s   n e e d e d . 
 
 N o t i c e   t h a t   t h e r e   a r e   t w o   a r e a   f o r   i n p u t : 
 
 * * ( 2 7 0 ,   4 2 5 ,   4 5 ,   4 5 ) * *   a n d   * * ( 3 2 5 ,   4 2 5 ,   4 5 ,   4 5 ) * *   a t   E v e n t   S t a t e m e n t . 
 
 N o t i c e   t h a t   a l s o   t h e r e   a r e   t w o   a r e a   f o r   o u t p u t . 
 
 * * ( m a r g i n ,   h e i g h t   -   r   - 1 0   ,   r ,   r ) * *   a n d   * * ( m a r g i n   +   r   +   r _ m a r g i n ,   h e i g h t   -   r   -   1 0 ,   r ,   r ) * *   a t   d r a w B u t t o n s . 
 
 I n   t h e   c a s e   o f   b u t t o n ,   i n p u t   a n d   o u t p u t   a r e a   f o r   b u t t o n   m u s t   b e   * * i d e n t i c a l * * .   ( O t h e r w i s e ,   t h i s   b u t t o n   w i l l   b e   d e c e p t i v e ! )   I t   w i l l   b e   b e s t   i d e a   t o   s e t   t h i s   d a t a   a s   s a m e   v a l u e   w i t h o u t   c a l c u l a t i n g   e x a c t   l o c a t i o n   o f   f u n c t i o n   ( f o r   o u t p u t )   i n t o   c o n s t a n t   ( f o r   i n p u t ) .   T h e r e   i s   n o   s p e c i f i c   f u n c t i o n   t o   t i e   t h i s   a r e a ,   s o   y o u   h a v e   t o   c a r e   a b o u t   t h i s . 
 
 
 
 < R e f e r e n c e   C o d e >   : : 
 
 
 
         i m p o r t   p y g a m e ,   s y s 
 
         f r o m   p y g a m e . l o c a l s   i m p o r t * 
 
         
 
         m a x H P   =   1 0   
 
         w h i t e   =   ( 2 5 5 , 2 5 5 , 2 5 5 ) 
 
         g r a y   =   ( 1 2 7 , 1 2 7 , 1 2 7 ) 
 
         b l a c k   =   ( 0 , 0 , 0 ) 
 
         r e d   =   ( 2 5 5 , 0 , 0 ) 
 
         g r e e n   =   ( 0 , 2 5 5 , 0 ) 
 
         b l u e   =   ( 0 , 0 , 2 5 5 ) 
 
         p y g a m e . i n i t ( ) 
 
         p y g a m e . d i s p l a y . s e t _ c a p t i o n ( " A r r a y   b u t t o n s   P r o j e c t " ) 
 
         w i d t h   =   6 4 0   
 
         h e i g h t   =   4 8 0 
 
         m y S c r e e n   =   p y g a m e . d i s p l a y . s e t _ m o d e ( ( w i d t h ,   h e i g h t ) ) 
 
         m y T e x t F o n t   =   p y g a m e . f o n t . F o n t ( " H o o n W h i t e c a t R . t t f " ,   3 2 ) 
 
         m y T e x t   =   m y T e x t F o n t . r e n d e r ( ( s t r ( m a x H P )   +   " / "   +   s t r ( m a x H P ) ) ,   T r u e ,   r e d ,   g r a y ) 
 
         m y T e x t A r e a   =   m y T e x t . g e t _ r e c t ( ) 
 
         m y T e x t A r e a . c e n t e r   =   ( w i d t h / 2 ,   h e i g h t / 2 ) 
 
         f p s C l o c k   =   p y g a m e . t i m e . C l o c k ( ) 
 
         
 
         d e f   m a i n ( ) : 
 
                 H P   =   5 
 
                 
 
                 w h i l e   T r u e : 
 
                         m y T e x t   =   m y T e x t F o n t . r e n d e r ( ( s t r ( H P )   +   " / "   +   s t r ( m a x H P ) ) ,   T r u e ,   r e d ,   g r a y ) 
 
                 
 
                         m y S c r e e n . f i l l ( g r a y ) 
 
         
 
                         m y S c r e e n . b l i t ( m y T e x t ,   m y T e x t A r e a ) 
 
                         d r a w H P ( H P ) 
 
                         d r a w B u t t o n s ( ) 
 
         
 
                         f o r   e v e n t   i n   p y g a m e . e v e n t . g e t ( ) : 
 
                                 i f   e v e n t . t y p e   = =   Q U I T : 
 
                                         p y g a m e . q u i t ( ) 
 
                                         s y s . e x i t ( ) 
 
                                 e l i f   e v e n t . t y p e   = =   K E Y D O W N : 
 
                                         i f   e v e n t . k e y   = =   K _ U P : 
 
                                                 i f   H P   ! =   1 0 : 
 
                                                         H P   =   H P   +   1 
 
                                         e l i f   e v e n t . k e y   = =   K _ D O W N : 
 
                                                 i f   H P   ! =   0 : 
 
                                                         H P   =   H P   -   1 
 
                                 e l i f   e v e n t . t y p e   = =   M O U S E B U T T O N U P :   # 1 
 
                                         x ,   y   =   e v e n t . p o s 
 
                                         i f   p y g a m e . R e c t ( 2 7 0 ,   4 2 5 ,   4 5 ,   4 5 ) . c o l l i d e p o i n t ( x ,   y ) : 
 
                                                 i f   H P   ! =   1 0 : 
 
                                                         H P   =   H P   +   1 
 
                                         e l i f   p y g a m e . R e c t ( 3 2 5 ,   4 2 5 ,   4 5 ,   4 5 ) . c o l l i d e p o i n t ( x ,   y ) : 
 
                                                 i f   H P   ! =   0 : 
 
                                                         H P   =   H P   -   1             
 
                 
 
                         p y g a m e . d i s p l a y . u p d a t e ( ) 
 
                         f p s C l o c k . t i c k ( 6 0 ) 
 
         
 
         d e f   d r a w H P ( H P ) : 
 
                 r   =   i n t ( ( h e i g h t   -   4 0 )   /   m a x H P ) 
 
         
 
                 p y g a m e . d r a w . r e c t ( m y S c r e e n ,   b l a c k ,   ( 2 0 ,   2 0 ,   2 0 ,   2 0   +   ( ( m a x H P   -   0 . 5 )   *   r ) ) ) 
 
         
 
                 f o r   i   i n   r a n g e ( m a x H P ) : 
 
                         i f   H P   > =   ( m a x H P   -   i ) : 
 
                                 p y g a m e . d r a w . r e c t ( m y S c r e e n ,   r e d ,   ( 2 0 ,   2 0   +   ( i   *   r ) ,   2 0 ,   r ) ) 
 
                         p y g a m e . d r a w . r e c t ( m y S c r e e n ,   w h i t e ,   ( 2 0 ,   2 0   +   ( i   *   r ) ,   2 0 ,   r ) ,   1 ) 
 
         
 
                 r e t u r n 
 
         
 
         d e f   d r a w B u t t o n s ( ) : 
 
                 r   =   4 5 
 
                 r _ m a r g i n   =   1 0 
 
                 c o l o r s   =   [ r e d ,   b l a c k ] 
 
                 
 
                 n u m   =   2 
 
                 m a r g i n   =   i n t ( ( w i d t h   -   ( ( r   *   n u m )   +   ( r _ m a r g i n   *   ( n u m   -   1 ) ) ) )   /   2 ) 
 
                 f o r   i   i n   r a n g e ( 0 ,   n u m ) : 
 
                         l e f t   =   m a r g i n   +   ( i   *   r )   +   ( i   *   r _ m a r g i n ) 
 
                         u p   =   h e i g h t   -   r   -   1 0 
 
                         p y g a m e . d r a w . r e c t ( m y S c r e e n ,   c o l o r s [ i ] ,   ( l e f t ,   u p ,   r ,   r ) ) 
 
                         p y g a m e . d r a w . r e c t ( m y S c r e e n ,   g r a y ,   ( l e f t   +   2 ,   u p   +   2 ,   r   -   4 ,   r   -   4 ) ,   2 ) 
 
         
 
         i f   _ _ n a m e _ _   = =   ' _ _ m a i n _ _ ' : 
 
                 m a i n ( ) 
 
         